package com.etennis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etennis.mapper.ClubMapper;
import com.etennis.mapper.MemberMapper;
import com.etennis.mapper.SeasonInfoMapper;
import com.etennis.pojos.dto.app.UserDTO;
import com.etennis.pojos.entity.Club;
import com.etennis.pojos.entity.Member;
import com.etennis.pojos.entity.Record;
import com.etennis.pojos.entity.Seasoninfo;
import com.etennis.pojos.vo.app.UserVO;
import com.etennis.service.IMemberService;
import com.etennis.service.IRecordService;
import com.etennis.utils.BeanCopyUtils;
import com.etennis.utils.RedisCache;
import com.etennis.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    ClubMapper clubMapper;
    @Autowired
    SeasonInfoMapper seasonInfoMapper;
    @Autowired
    IRecordService recordService;
    @Autowired
    RedisCache redisCache;

    @Override
    public UserVO mine() {
        Long id = SecurityUtils.getLoginUserId();
        Member member = memberMapper.selectById(id);
        UserVO userVO = BeanCopyUtils.copyBean(member, UserVO.class);
        Club club = clubMapper.selectById(member.getClubId());
        userVO.setClubName(club.getName());

        Long seasonId= redisCache.getCurSeason();
        Seasoninfo seasoninfo = seasonInfoMapper.selectOne(new LambdaQueryWrapper<Seasoninfo>().eq(Seasoninfo::getMemberId, id).eq(Seasoninfo::getSeasonId, seasonId));
        userVO.setGroupId(seasoninfo.getGroupId());
       userVO.setWinMatchRate(50);
       userVO.setGroupCount(16);
       userVO.setWinSetRate(45);
       userVO.setSetCount(10);
       userVO.setMatchCount(20);
       userVO.setGroupRank(5);
        return userVO;
    }

    @Override
    public void updateUser(UserDTO userDTO) {
        Member member = BeanCopyUtils.copyBean(userDTO, Member.class);
        memberMapper.updateById(member);
    }

}
